//
//  DriverManagerListVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/5.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DriverManagerListVC.h"
#import "DriverMainModel.h"
#import "DriverListCell.h"
#import "DriverEditVC.h"
#import "DriverAddVC.h"

@interface DriverManagerListVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <DriverMainModel *>*array;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation DriverManagerListVC

-(NSMutableArray<DriverMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 30 - 15 - 30) * 0.5, 44)];
        [nextBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"邀请司机注册" forState:0];
        [nextBtn setTitleColor:HexColor(0x3D68EB) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(inviteClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
        
        UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [saveBtn setBackgroundColor:Color_ThemeColor];
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 6;
        [saveBtn setTitle:@"添加司机" forState:0];
        [saveBtn setTitleColor:HexColor(0xffffff) forState:0];
        saveBtn.titleLabel.font = BoldFontOfSize(16);
        [saveBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:saveBtn];
    }
    return _bottomView;
}

- (void)inviteClick {
    
}

- (void)addClick {
    DriverAddVC *vc = [DriverAddVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight)];
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"司机管理";
    page = 1;
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    [editBtn setTitle:@"编辑" forState:0];
    [editBtn setTitleColor:HexColor(0x9C9DB3) forState:0];
    editBtn.titleLabel.font = BoldFontOfSize(14);
    [editBtn addTarget:self action:@selector(editClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:editBtn]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestForList {
    [HttpRequest PostWithApi:LogisticsSupplierDriverList_V_1_0 params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [DriverMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [DriverMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)editClick {
    DriverEditVC *vc = [DriverEditVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DriverListCell *cell = [DriverListCell DriverListCellWithTableView:tableView];
    DriverMainModel *model = self.array[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.operationBlock = ^{
        if (model.status == DriverStatusCanUse) {
            [weakself driverOperation:2 With:model.Id];
        }else if (model.status == DriverStatusNoUse) {
            [weakself driverOperation:1 With:model.Id];
        }
    };
    return cell;
}

- (void)driverOperation:(int)type With:(NSString *)driverId {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:type == 1 ? @"确定启用？" : @"确定禁用？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self requestForOperation:type With:driverId];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)requestForOperation:(int)type With:(NSString *)driverId{
    //1=启用，2=禁用，3=删除
    [HttpRequest PostWithApi:LogisticsSupplierDriverOperation_V_1_0 params:@{@"ids":driverId,@"status":@(type)} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:type == 1 ? @"已启用" : @"已禁用"];
//            [self requestForList];
            [self.tableView.mj_header beginRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DriverListCell *cell = [DriverListCell DriverListCellWithTableView:tableView];
    return cell.height;
}

@end
